<?php
namespace app\admin\controller;
use app\model\User as UserModel;
use app\model\Admin as AdminModel;
use think\Db;
use app\model\UserBill as UserBillModel;

use app\lib\BceSignService as BceSignService;
use app\lib\BceApi as BceApi;

// use app\lib\aliyun\Api as aliyunApi;
// use AlibabaCloud\Client\AlibabaCloud;
// use AlibabaCloud\Client\Exception\ClientException;
// use AlibabaCloud\Client\Exception\ServerException;

class User extends Base
{
    public function __construct(){
        $model = new UserModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '用户';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;

        $page_info = $this->page_info;
        $fields = [
            // 'account'=>['name'=>'用户名','type'=>'text','valids'=>['required']],
            'mobile'=>['name'=>'手机号码','type'=>'text','valids'=>['required']],
            'password'=>['name'=>'密码','type'=>'random_text','valids'=>['required']],
            'truename'=>['name'=>'姓名','type'=>'text','valids'=>['required']],
            'email'=>['name'=>'邮箱','type'=>'text'],
        ];
        if ($page_info['action'] == 'edit') {
            $fields['password']['placeholder'] = '留空，则不修改';
            $fields['password']['value'] = '';
            unset($fields['password']['valids']);
        }
        
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);
    }


    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like'],
            ['key'=>'mobile','type'=>'='],
            ['key'=>'email','type'=>'='],
            ['key'=>'status','type'=>'='],
            ['key'=>'id','type'=>'='],
        ];
        $map = getSearchParam($map_params);

        $search_params = [
            [
                ['key'=>'name','name'=>'姓名','type'=>'text'],
                ['key'=>'mobile','name'=>'手机','type'=>'text'],
                ['key'=>'email','name'=>'邮箱','type'=>'text'],
                // ['key'=>'status','name'=>'状态','type'=>'select1_1','params'=>$model->getStatusList()]
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];

        $order_field = input('order_field','id');
        $order_type = input('order_type','desc');
        $list = $model->where($map)->order($order_field,$order_type)->paginate($page_param);
        $page = $list->render();

        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            
        }

        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function export(){
        set_time_limit ( 0 );
        ini_set('memory_limit','40960M');

        $map_params = [
            ['key'=>'name','type'=>'like'],
            ['key'=>'mobile','type'=>'='],
            ['key'=>'email','type'=>'='],
            ['key'=>'status','type'=>'='],
            ['key'=>'admin_id','type'=>'='],
            ['key'=>'pk','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        $map[] = ['is_delete','=',0];

        $model = $this->model;
        $list = $model->where($map)->order('id','asc')->select();   

        $admins = AdminModel::select();
        $admins = arrayParamExtract($admins,'id','truename');
        
        $data = [];
        $title = ['类型','名称','手机','邮箱','证件类型','证件号','余额','pk','状态','冻结','托管','总折扣','所属销售','创建时间','备注'];
        foreach ($list as $k => $vo) {
            $tmp = [
                $model->getAccountAttrCn($vo['account_attr']),
                $vo['name'],$vo['mobile'],$vo['email'],
                $model->getLicenseTypeCn($vo['license_type']),
                $vo['license_number'],$vo['balance'],$vo['pk']."\t",
                $model->getStatusCn($vo['status']),
                $model->getCommonIsCn($vo['is_freeze']),
                $model->getCommonIsCn($vo['is_trusteeship']),
                $vo['total_discount'],
                $admins[$vo['admin_id']],
                timeDef($vo['ctime'],'','Y-m-d H:i:s'),
                $vo['remark']
            ];
            $data[] = $tmp;
        }
        exportCsv(date('YmdHis'),$title,$data);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['mobile','password','truename'],
                'nomust'=>['email']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            $save_data['password'] = md5($save_data['password']);

            $save_data['sk'] = uuidCreate();
        }


        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        if (request()->isPost()) {
            $fields = [
                'must'=>[],
                'nomust'=>['password','truename','mobile','email']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            if (!empty($save_data['password'])) {
                $save_data['password'] = md5($save_data['password']);
            }else{
                unset($save_data['password']);
            }
        }
        return parent::updateData($id,$save_data);
    }


  

    
   
}
